//
//  LKTabBar.swift
//  LKWeibo
//
//  Created by like on 16/8/9.
//  Copyright © 2016年 like. All rights reserved.
//

import UIKit

protocol LKTabBarDelegate:NSObjectProtocol {
    func tabBarClick()
}


class LKTabBar: UITabBar {
    
    
    weak var LKDelegaer:LKTabBarDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //设置视图
    private func setupUI(){
        addSubview(but)
        
    }
    
    //给子控件设置frame的时候 走这个方法   不能再这里 Snap 设置约束 
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let childW = self.frame.width * 0.2
        var index: CGFloat = 0
        
        
        for value in self.subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                value.frame.size.width = childW
                value.frame.origin.x = index * childW
                index += 1
                if index == 2 {
                    index = 3
                }
            }
            
        }
        
        but.center.x = frame.width * 0.5
        but.center.y = self.frame.height * 0.5
    }
    //button的 点击事件
    func buttonClick(){
        LKDelegaer?.tabBarClick()
                
    }
    
    
    //懒加载控件 button
    private lazy var but:UIButton = {
        
        let butt = UIButton()
        butt.addTarget(self, action: #selector(LKTabBar.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        butt.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        butt.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        //tabbar_compose_icon_add_highlighted
        butt.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        butt.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        
        
        //自适应
        butt.sizeToFit()
        
        return butt
    }()
    
}
